package cn.kgc.springboot.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis操作String类型值的工具类
 * @author mosin
 * date 2021/12/15
 * @version 1.0
 */
@Component
public final class RedisUtil {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    private RedisUtil(){}

    //添加string的值
    public void setString(String key,String value){
        redisTemplate.opsForValue().set(key, value );
    }
    // 设置有效时间的String
    public void setString(String key, String value, long timeout, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key, value ,timeout,timeUnit);
    }

    // 获取key的剩余存活时间
    public Long getLifeCycle(String key){
       return redisTemplate.getExpire(key);
    }

    //判定key 是否存在  存在 不设置
    public void setIfAbSent(String key,String value){

        redisTemplate.opsForValue().setIfAbsent(key, value);

    }

    //判定key 是否存在  存在 不设置
    public void setIfAbSent(String key,String value, long timeout, TimeUnit timeUnit){
        redisTemplate.opsForValue().setIfAbsent(key, value,timeout,timeUnit);
    }

    //删除值
    public Boolean delStr(String key){
        return redisTemplate.delete(key);
    }

    //获取值
    public String getStr(String key){
        return  redisTemplate.opsForValue().get(key);
    }




}
